package system

import (
	"context"
	"github.com/gogf/gf/v2/net/ghttp"
	"server/internal/model/entity"
	"server/internal/service"
	"strconv"
)

const (
	CookieLoginAdminID       = "loginAdminID"
	CookieLoginAdminUsername = "loginAdminUsername"
	CookieLoginAdminName     = "loginAdminName"
)

type sSystemCookie struct {
}

func init() {
	service.RegisterSystemCookie(&sSystemCookie{})
}

func (s *sSystemCookie) SetUser(ctx context.Context, admin *entity.SystemUser) {
	//cookiePath := ghttp.RequestFromCtx(ctx).Server.GetCookiePath()
	cookiePath := ""
	maxAge := ghttp.RequestFromCtx(ctx).Server.GetCookieMaxAge()
	//cookieDomain := ghttp.RequestFromCtx(ctx).Server.GetCookieDomain()
	cookieDomain := ""
	options := ghttp.CookieOptions{}
	ghttp.RequestFromCtx(ctx).Cookie.SetCookie(CookieLoginAdminID, strconv.Itoa(int(admin.Id)), cookiePath, cookieDomain, maxAge, options) //前端通过判断是否存在作为登录状态
	ghttp.RequestFromCtx(ctx).Cookie.SetCookie(CookieLoginAdminUsername, admin.Username, cookiePath, cookieDomain, maxAge, options)
	ghttp.RequestFromCtx(ctx).Cookie.SetCookie(CookieLoginAdminName, admin.Name, cookiePath, cookieDomain, maxAge, options)
}

func (s *sSystemCookie) RemoveAll(ctx context.Context) {
	ghttp.RequestFromCtx(ctx).Cookie.Remove(CookieLoginAdminID)
	ghttp.RequestFromCtx(ctx).Cookie.Remove(CookieLoginAdminUsername)
	ghttp.RequestFromCtx(ctx).Cookie.Remove(CookieLoginAdminName)
}
